﻿'******************************* File Header *********************************
'* File Name:    WmiConnectionTest.vb
'* Project:      WmiConnection.Test
'* Copyright (c) Freddy Kuehne
'*
'* Unit tests for WmiConnection.
'* 
'* This source is subject to the GNU Library General Public License (LGPL).
'* See http://wmiconnection.codeplex.com/license.
'* All other rights reserved.
'*****************************************************************************
Imports System.Text
Imports System.Data.Common

<TestClass()>
Public Class WmiConnectionTest
#Region "Tests"
  <TestMethod()>
  Public Sub Constructor1Test()
    Using target As New WmiConnection
      Assert.IsTrue(String.IsNullOrEmpty(target.ConnectionString))
    End Using
  End Sub

  <TestMethod()>
  Public Sub Constructor2Test()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      Assert.AreEqual(csb.ConnectionString, target.ConnectionString)
    End Using
  End Sub

  <TestMethod()>
  Public Sub Constructor3Test()
    Using target As New WmiConnection(Nothing)
      Assert.IsTrue(String.IsNullOrEmpty(target.ConnectionString))
    End Using
  End Sub

  <TestMethod()>
  Public Sub HierarchyTest()
    Using target As New WmiConnection()
      Assert.IsInstanceOfType(target, GetType(DbConnection))
    End Using
  End Sub

  <TestMethod()>
  Public Sub BeginTransaction1Test()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      target.Open()
      Using tran As IDbTransaction = target.BeginTransaction()
        Assert.IsNotNull(tran)
        Assert.AreEqual(GetType(WmiTransaction), tran.GetType)
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub BeginTransaction2Test()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      target.Open()
      For Each en As IsolationLevel In [Enum].GetValues(GetType(IsolationLevel))
        Using tran As IDbTransaction = target.BeginTransaction(en)
          Assert.IsNotNull(tran)
        End Using
      Next
    End Using
  End Sub

  <TestMethod(), ExpectedException(GetType(InvalidOperationException))>
  Public Sub BeginTransaction1ExceptionTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      Using tran As IDbTransaction = target.BeginTransaction()
      End Using
    End Using
  End Sub

  <TestMethod(), ExpectedException(GetType(InvalidOperationException))>
  Public Sub BeginTransaction2ExceptionTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      Using tran As IDbTransaction = target.BeginTransaction(IsolationLevel.ReadCommitted)
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub ChangeDatabaseTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      target.Open()
      Assert.AreEqual("\root\cimv2", target.Database)
      target.ChangeDatabase("\root")
      Assert.AreEqual("\root", target.Database)
    End Using
  End Sub

  <TestMethod(), ExpectedException(GetType(ArgumentNullException))>
  Public Sub ChangeDatabaseException1Test()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      target.Open()
      target.ChangeDatabase(Nothing)
    End Using
  End Sub

  <TestMethod(), ExpectedException(GetType(ArgumentNullException))>
  Public Sub ChangeDatabaseException2Test()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      target.Open()
      target.ChangeDatabase(String.Empty)
    End Using
  End Sub

  <TestMethod(), ExpectedException(GetType(InvalidOperationException))>
  Public Sub ChangeDatabaseException3Test()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      target.ChangeDatabase("\root")
    End Using
  End Sub

  <TestMethod()>
  Public Sub Close1Test()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      target.Open()
      Assert.AreEqual(ConnectionState.Open, target.State)
      target.Close()
      Assert.AreEqual(ConnectionState.Closed, target.State)

      ' no exception
      target.Close()
      Assert.AreEqual(ConnectionState.Closed, target.State)
    End Using
  End Sub

  <TestMethod()>
  Public Sub Close2Test()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      ' no exception
      target.Close()
      Assert.AreEqual(ConnectionState.Closed, target.State)
    End Using
  End Sub

  <TestMethod()>
  Public Sub ConnectionStringTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      Assert.AreEqual(csb.ConnectionString, target.ConnectionString)
      target.ConnectionString = Nothing
      Assert.IsTrue(String.IsNullOrEmpty(target.ConnectionString))
      target.ConnectionString = csb.ConnectionString
      Assert.AreEqual(csb.ConnectionString, target.ConnectionString)
      target.ConnectionString = String.Empty
      Assert.IsTrue(String.IsNullOrEmpty(target.ConnectionString))
    End Using
  End Sub

  <TestMethod(), ExpectedException(GetType(ArgumentException))>
  Public Sub ConnectionStringExceptionTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      target.ConnectionString = "invalidconstring"
    End Using
  End Sub

  <TestMethod()>
  Public Sub ConnectionTimeoutTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      Assert.AreEqual(15, target.ConnectionTimeout)
    End Using
  End Sub

  <TestMethod()>
  Public Sub CreateCommandTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      Using cmd As DbCommand = target.CreateCommand
        Assert.IsNotNull(cmd)
        Assert.AreEqual(GetType(WmiCommand), cmd.GetType)
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub DatabaseTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      target.Open()
      Assert.AreEqual("\root\cimv2", target.Database)
    End Using
  End Sub

  <TestMethod()>
  Public Sub DataSourceTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      Assert.AreEqual("localhost", target.DataSource)
    End Using
  End Sub

  <TestMethod()>
  Public Sub OpenTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      target.Open()

      ' no Exception
      target.Open()
      Assert.AreEqual(ConnectionState.Open, target.State)
    End Using
  End Sub

  <TestMethod()>
  Public Sub ServerVersionTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      target.Open()
      Assert.IsFalse(String.IsNullOrEmpty(target.ServerVersion))
    End Using
  End Sub

  <TestMethod()>
  Public Sub StateTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using target As New WmiConnection(csb.ConnectionString)
      Assert.AreEqual(ConnectionState.Closed, target.State)
      target.Open()
      Assert.AreEqual(ConnectionState.Open, target.State)
      target.Close()
      Assert.AreEqual(ConnectionState.Closed, target.State)
    End Using
  End Sub
#End Region
End Class
